# encoding: utf-8
import sys
import copy
from OAT import *

#正交实验设计
def compute_oat(factor_level_table):
    oat=OAT()
    # 默认mode=0，宽松模式，只裁剪重复测试集（测试用例参数值可能为None）
    # mode=1，严格模式，除裁剪重复测试集外，还裁剪含None测试集(num为允许None测试集最大数目)
    data=(oat.genSets(factor_level_table,mode=1,num=0))
    # 将输出的结果data重新按输入的因素顺序排序
    sort_data=[]
    for one_data in data:
        #tmp=OrderedDict()
        tmp=[]
        for two_data in factor_level_table:
            tmp.append(one_data[two_data])
        print(tmp)
        sort_data.append(tmp)
    return sort_data

#全因子实验设计
def compute_full_factor(factor_level_table,output_data,one_data,idx):
    if idx>=len(factor_level_table):
        output_data.append(copy.deepcopy(one_data))
        return
    n=0
    levels=[]
    for key,val in factor_level_table.items():
        if n==idx:
            levels=val
            break
        n=n+1
    
    for level in levels:
        one_data.append(level)
        compute_full_factor(factor_level_table,output_data,one_data,idx+1)
        one_data.pop()


if __name__ == "__main__":
    inputpath = sys.argv[1]
    outputpath = sys.argv[2]
    computemod = sys.argv[3]
    print ("input path is:"+inputpath)
    print ("output path is:"+outputpath)
    print ("computemod path is:"+computemod)

    #with open('.\input.txt', 'r') as f:
    with open(inputpath, 'r') as f:
        factors = []
        for line in f.readlines():
            line = line.strip()
            if line:
                factor, levels = line.split(':')
                levels = levels.split(' ')
                factors.append((factor.strip(), levels))
    print(factors)
    factor_level_table = OrderedDict(factors)
    output_data=[]
    
    if computemod=="full":
        one_data=[]
        compute_full_factor(factor_level_table,output_data,one_data,0)
    else:
    #if computemod=="oat": 默认为正交实验设计
        output_data=compute_oat(factor_level_table)

    lines=[]
    line = ''
    # 将输出写入txt
    #with open(".\output.txt", "w", encoding="utf-8") as f:
    with open(outputpath, "w", encoding="utf-8") as f:
        #第一行写因素名
        for k,v in factor_level_table.items():
            line += k+' '
        lines.append(line)
        for val in output_data:
            # 使用 join 函数将 temp 和 element_Label 以制表符为分隔符连接到一起
            line = ' '.join([str(x) for x in val])
		    # 将字符串添加到字符串列表中
            lines.append(line)
        result = '\n'.join(lines)
        f.write(result)

